<?php

namespace app\admin\controller;

use houdunwang\arr\Arr;
use think\Controller;


class Article extends Controller
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Article();
    }
    //文章主页
    public function index(){
        //关联表
        $data = db('article')//被关联的表
            ->alias('a')//被关联表的别称
            ->join('__CATEGORY__ c','a.category_cid = c.cid')//要关联的表
            ->order('sendtime desc')//排序
            ->where('is_recycle',1)//条件
            ->paginate(3);//分页
        $this->assign('data',$data);
        return view();
    }
    //添加文章
    public function add(){
        //2获取所属分类
        $cateData = db('category')->order('cid desc')->select();
        $treeData = Arr::tree($cateData,'cname');
        $this->assign('treeData',$treeData);
        //3获取标签
        $tagData =db('tag')->select();
        $this->assign('tagData',$tagData);
        //4判断
        if(request()->isPost()){
            $data=input('post.');
            $res=$this->db->add($data);
            if($res['valid']){
                $this->success($res['msg'],'index');
            }else{
                $this->error($res['msg']);
            }
        }
        //1视图
        return view();
    }
    //编辑
    public function edit(){
        $aid = input('param.aid');
        //1.获取旧数据
        $oldData = db('article')
            ->alias('a')
            ->join('__ARTICLE_DATA__ ad','a.aid = ad.article_aid')
            ->find($aid);
        $this->assign('oldData',$oldData);
        //2.处理所属分类
        $cateData = db('category')->order('cid desc')->select();
        $treeData = Arr::tree($cateData,'cname');
        $this->assign('treeData',$treeData);
        //3.处理标签
        $tagData = db('tag')->select();
        $this->assign('tagData',$tagData);
        //4.获取所有选中的标签
        $checkTag = db('article_tag')->where('article_aid',$aid)->column('tag_tid');
        $this->assign('checkTag',$checkTag);
        if(request()->isPost()) {
            $data = input('post.');
            $res = $this->db->edit($data);
            if ($res['valid']) {
                $this->success($res['msg'], 'index');
            } else {
                $this->error($res['msg']);
            }
        }
        //视图
        return view();
    }
    //回收站
    public function recycle(){
        $aid=input('param.aid');
        //获取数据
        $data = db('article')//被关联的表
        ->alias('a')//被关联表的别称
        ->join('__CATEGORY__ c','a.category_cid = c.cid')//要关联的表
        ->order('sendtime desc')//排序
        ->where('is_recycle',0)//条件
        ->paginate(3);//分页
        $this->assign('data',$data);
        //放入回收站
        $res = $this->db->recycle($aid);
        if ($res['valid']){
            $this->success($res['msg'],'index');
        }
        //视图
        return view();
    }
    //还原
    public function reduction(){
        $aid=input('param.aid');
        $res = $this->db->reduction($aid);
        if ($res['valid']){
            $this->success($res['msg'],'recycle');
        }
        //视图
        return view();
    }
    //删除
    public function del(){
        $aid=input('param.aid');
        $res = $this->db->del($aid);
        if ($res['valid']){
            $this->success($res['msg'],'recycle');
        }
    }
}
